package com.lemon.stock.dao

import androidx.room.*
import com.lemon.stock.entity.Trade

@Dao
interface TradeDao {

    @Query("SELECT * FROM trade WHERE id= :id")
    fun entity(id: Long): Trade

    @Query("SELECT * FROM trade WHERE stock_id= :stockId AND status=10 ORDER BY buy_date DESC")
    fun historyTrades(stockId: String): List<Trade>

    @Query("SELECT * FROM trade WHERE stock_id= :id AND invest_index=:index AND status BETWEEN 5 AND 9 ORDER BY buy_date DESC")
    fun stockTrades(id: String, index: Int): List<Trade>

    @Query("SELECT * FROM trade WHERE stock_id= :id AND invest_index=:index AND status BETWEEN 5 AND 10 ORDER BY buy_date ASC")
    fun surveyTrades(id: String, index: Int): List<Trade>

    @Query("SELECT * FROM trade WHERE stock_id= :id AND status BETWEEN 5 AND 10 ORDER BY invest_index ASC, buy_date ASC")
    fun surveyTrades(id: String): List<Trade>

    @Query("SELECT * FROM trade WHERE stock_id= :stockId AND status=8 ORDER BY buy_date ASC")
    fun holdTrades(stockId: String): MutableList<Trade>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(vararg trade: Trade)

    @Update
    fun update(vararg trade: Trade)

    //
    @Query("UPDATE trade SET status=10 WHERE stock_id= :id AND invest_index=:index AND status = 9")
    fun endBatch(id: String, index: Int):Int

    @Query("DELETE FROM trade")
    fun clear()
}